package com.tjhk.design;

import java.io.Serializable;

public class Singleton implements Serializable {
    private volatile static Singleton instance = null;
    private static boolean singletonFlag = false;
    private Singleton() {
        if (instance != null || singletonFlag) {
            throw new RuntimeException("试图用反射破坏异常");
        }
        singletonFlag = true;
    };
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

}
